home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat46 / digiz / digizp.c < prev   
Encoding:
C/C++ Source or Header  |  1978-01-01  |  12.3 KB  |  590 lines

  1. /*
  2. lc DigiZP
  3. Blink FROM LIB:cback.o+ ram:DigiZP.o TO DigiZ LIB lib:lc.lib lib:amiga.lib DEFINE __main=__tinymain NODEBUG SMALLCODE SMALLDATA VERBOSE
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <exec/types.h>
  9. #include <exec/io.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <workbench/workbench.h>
  13. #include <workbench/icon.h>
  14. #include <workbench/startup.h>
  15. #include <graphics/rastport.h>
  16. #include <graphics/text.h>
  17. #include <devices/timer.h>
  18. #include <libraries/dos.h>
  19. #include <proto/dos.h>
  20. #include <dh1:fichiers/c/digizV2/Interface.c>
  21.  
  22. #define clk(x) ((x)==1?2:5)
  23. #define spc(x) ((x)==1?30:42)
  24. #define zero(x) ((x)==' '?'0':(x))
  25.  
  26. /* #define min(x,y) ((x)<(y)?(x):(y)) */
  27. #define conf(max,x) (((x)<(max))&&((x)>=0)?TRUE:FALSE)
  28.  
  29. char *_procname="DigiZ";
  30. long _priority=20,_BackGroundIO=1,_stack=4000;
  31. extern BPTR _Backstdout;
  32. UBYTE vers[]="\0$VER: DigiZ v2.0 (16.07.93)";
  33.  
  34. struct RastPort *RPort;
  35. struct Screen *screen;
  36. struct Screen *ascreen;
  37. struct Window *Window;
  38. struct Window *Window2=NULL;
  39. struct IntuitionBase *IntuitionBase;
  40. struct IconBase *IconBase;
  41. struct DiskObject *DiskObj;
  42. struct IntuiMessage *Message;
  43. struct MsgPort *TimerPort;
  44. struct timerequest *TimeReq;
  45.  
  46. char av[250],alert[110],tm2[10],a2[40]={ 1,1,105,67,
  47.         "DigiZ a été créé par Mikaël Zajac",'\0'};
  48. BOOL late=FALSE,wb2,win2=FALSE,reb;
  49. int d=1,l=0,duree=15;
  50.  
  51. struct temps
  52. {
  53.     int sec,min,hr,mic;
  54. };
  55.  
  56.  
  57. void init()
  58. {
  59.     ULONG lock;
  60.     if((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL) fin(1);
  61.     if((TimerPort=(struct MsgPort *)CreatePort(0,0))==NULL) fin(2);
  62.     if((TimeReq=(struct timerequest *)CreateExtIO(TimerPort,sizeof(struct timerequest)))==NULL) fin(3);
  63.     if(OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimeReq,NULL)!=0) fin(4);
  64.     lock=LockIBase(0);
  65.     screen=IntuitionBase->FirstScreen;
  66.     wb2=(IntuitionBase->LibNode.lib_Version>=37);
  67.     if(wb2)
  68.         NewWindow.Width=127;
  69.     else
  70.         NewWindow.IDCMPFlags=NewWindow.IDCMPFlags|ACTIVEWINDOW|INACTIVEWINDOW;
  71.     UnlockIBase(lock);
  72.     ascreen=screen;
  73. }
  74.  
  75. int fin(l)
  76. int l;
  77. {
  78.     switch(l)
  79.     {
  80.         case 4:    CloseDevice(TimeReq);
  81.         case 3:    DeleteExtIO(TimeReq);
  82.         case 2:    DeletePort(TimerPort);
  83.         case 1:    CloseLibrary(IntuitionBase);
  84.             break;
  85.     }
  86.     exit(0);
  87.     return(0);
  88. }
  89.  
  90. void ffen()
  91. {
  92.     CloseWindow(Window);
  93.     Window=NULL;
  94. }
  95.  
  96. void stop()
  97. {
  98.     if( !CheckIO(TimeReq) )
  99.          AbortIO(TimeReq);
  100.     WaitIO(TimeReq);  
  101. }
  102.  
  103. void fenetre2()
  104. {
  105.     NewWindow2.Screen=screen;
  106.     NewWindow2.TopEdge=(screen->Height-87)/2;
  107.     NewWindow2.LeftEdge=(screen->Width-300)/2;
  108.     if((Window2=(struct Window *)OpenWindow(&NewWindow2))==NULL)
  109.     {
  110.         stop();
  111.         ffen();
  112.         fin(4);
  113.     }
  114.     if(wb2)
  115.         ModifyIDCMP(Window,MOUSEBUTTONS);
  116.     else
  117.         ModifyIDCMP(Window,ACTIVEWINDOW|INACTIVEWINDOW);
  118.     PrintIText(Window2->RPort,&IText6,4,11); 
  119. }
  120.     
  121. void ffen2()
  122. {
  123.     if(wb2)
  124.         ModifyIDCMP(Window,CLOSEWINDOW|MOUSEBUTTONS);
  125.     else
  126.         ModifyIDCMP(Window,CLOSEWINDOW|MOUSEBUTTONS|ACTIVEWINDOW|INACTIVEWINDOW);
  127.     CloseWindow(Window2);
  128.     Window2=NULL;
  129. }
  130.  
  131. int fenetre()        /* €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ */
  132. {
  133.     if (screen==NULL) screen=ascreen;
  134.     NewWindow.Screen=screen;
  135.     NewWindow.LeftEdge=screen->Width-200;
  136.     if((Window=(struct Window *)OpenWindow(&NewWindow))==NULL)
  137.         fin(4);
  138.     RPort=Window->RPort;
  139.     ScreenToFront(screen);
  140.     if(win2)
  141.     {
  142.         win2=FALSE;
  143.         fenetre2();
  144.     }
  145.     return(0);
  146. }
  147.  
  148. struct temps *Heure()
  149. {
  150.     struct temps *h;
  151.     h=(struct temps *) malloc(sizeof(struct temps));
  152.     TimeReq->tr_node.io_Command=TR_GETSYSTIME;
  153.     DoIO(TimeReq);
  154.     h->sec=TimeReq->tr_time.tv_secs;
  155.     h->min=h->sec/60;
  156.     h->hr=h->min/60;
  157.     h->min%=60;
  158.     h->hr%=24;
  159.     h->sec%=60;
  160.     h->mic=TimeReq->tr_time.tv_micro;
  161.     return(h);
  162. }
  163.     
  164.  
  165. void launchtimer(l)
  166. int l;
  167. {
  168.     struct temps *h;
  169.     h=Heure();
  170.     TimeReq->tr_node.io_Command=TR_ADDREQUEST;
  171.     TimeReq->tr_time.tv_secs=(1000000-h->mic)/1000000;
  172.     TimeReq->tr_time.tv_micro=(1000000-h->mic)%1000000;
  173.     if(l==0) TimeReq->tr_time.tv_secs+=(59-h->sec);
  174.     SendIO(TimeReq);
  175.     free(h); 
  176. }
  177.  
  178. void MakeAlertMsg()
  179. {
  180.     int i,j;
  181.     alert[0]=0;
  182.     alert[1]=95;
  183.     alert[2]=37;
  184.     for(i=0;themsgSIBuff[i]!='\0';alert[i+3]=themsgSIBuff[i],i+=1);
  185.     alert[i+3]='\0';
  186.     for(j=0;a2[j]!='\0';i+=1,alert[i+3]=a2[j],j+=1);
  187. }
  188.  
  189. BOOL conforme(max,adr)
  190. int max;
  191. struct StringInfo *adr;
  192. {
  193.     return( (BOOL) conf(max,adr->LongInt));
  194. }
  195.  
  196. BOOL rebours()
  197. {
  198.     BOOL Late=FALSE;
  199.     struct temps *h;
  200.     h=Heure();
  201.     if((h->hr*3600+h->min*60+h->sec*l-(secSInfo.LongInt*l+minSInfo.LongInt*60+heureSInfo.LongInt*3600)<duree*50) && (onoff.Flags & SELECTED))
  202.         Late=TRUE;
  203.     free(h);
  204.     Delay(duree*50);
  205.     return(Late);
  206. }
  207.  
  208. struct temps *Time()
  209. {
  210.     struct temps *h;
  211.  
  212.     h=Heure();
  213.     if(l==1)
  214.     {
  215.         sprintf(tm2,"%2d:%2d:%2d\0",h->hr,h->min,h->sec);
  216.         tm2[0]=zero(tm2[0]);
  217.         tm2[3]=zero(tm2[3]);
  218.         tm2[6]=zero(tm2[6]);
  219.     }
  220.     if(!wb2)
  221.     {
  222.         MyIT.IText=&tm2[0];
  223.         if(l==0)
  224.         {
  225.             sprintf(tm2,"%2d:%2d\0",h->hr,h->min);
  226.             tm2[0]=zero(tm2[0]);
  227.             tm2[3]=zero(tm2[3]);
  228.         }
  229.         DrawImage(RPort,&Image1,0,0);
  230.         PrintIText(RPort,&MyIT,spc(l),1);
  231.     }
  232.     else
  233.     {
  234.         if(l==0)
  235.         {
  236.             sprintf(tm2," %2d:%2d\0",h->hr,h->min);
  237.             tm2[1]=zero(tm2[1]);
  238.             tm2[4]=zero(tm2[4]);
  239.         }
  240.         SetWindowTitles(Window,tm2,-1);
  241.     }
  242.     return(h);
  243. }
  244.  
  245. BOOL fonction(g,l)
  246. int g,l;
  247. {
  248.     BOOL Late=FALSE;
  249.     ULONG lock;
  250.  
  251.     switch(g)
  252.     {
  253.         case 1:    ffen2();
  254.             break;
  255.         case 3: stop();
  256.             ffen2();
  257.             ffen();
  258.             Late=rebours();
  259.             lock=LockIBase(0);
  260.             screen=IntuitionBase->FirstScreen;
  261.             UnlockIBase(lock);
  262.         case 2:
  263.             if(Window2!=NULL) ffen2();
  264.             if(Window!=NULL) ffen();    
  265.             if(g==2)
  266.             {
  267.                 lock=LockIBase(0);
  268.                 screen=ascreen->NextScreen;
  269.                 UnlockIBase(lock);
  270.             }
  271.             fenetre();
  272.             fenetre2();
  273.             if(g==2) stop();
  274.             break;
  275.     }
  276.     return(Late);
  277. }
  278.  
  279. void oops(Gad,SI)
  280. struct Gadget *Gad;
  281. struct StringInfo *SI;
  282. {
  283.     AutoRequest(Window, &my_body_text, NULL, &my_ok_text, NULL, NULL, 200, 50);
  284.     strcpy(SI->Buffer,SI->UndoBuffer);
  285.     SI->NumChars=strlen(SI->Buffer);
  286.     SI->BufferPos=SI->NumChars;
  287.     ActivateWindow(Window2);
  288.     RefreshGadgets(&chscr,Window2,NULL);
  289.     ActivateGadget(Gad,Window2,NULL);
  290. }
  291.  
  292. void fantom()
  293. {
  294.     onoff.Flags=GADGHBOX;
  295.     heure.Flags=GADGHCOMP;
  296.     min.Flags=GADGHCOMP;
  297.     if(mode.Flags & SELECTED)
  298.         sec.Flags=GADGHCOMP;
  299. }
  300.  
  301.  
  302. void Args(int argc, char **argv)
  303. {
  304.     int i=0,aa=0,bb=0,cc=0;
  305.     struct WBStartup *WBS;
  306.     
  307.     char opt[9][5]={"-sec","-on","-@","-m","-h","?","-fen","-w","-d"},
  308.          opt2[7][9]={"SEC","ALARME","MSG","HEURE","FENETRE","DUREE","ATTENDRE"},*Value;
  309.     BOOL muet=FALSE,Sec=FALSE,Alm=FALSE,Hr=FALSE;    
  310.  
  311.     
  312.     if(argc!=0)
  313.     {
  314.         for(i=0;i<=argc-1;i+=1)
  315.         {
  316.             if(stricmp(argv[i],opt[0])==0) Sec=TRUE;
  317.             
  318.             if(stricmp(argv[i],opt[1])==0) Alm=TRUE;
  319.             
  320.             if(stricmp(argv[i],opt[2])==0)
  321.                 muet=TRUE;
  322.             if(strnicmp(argv[i],opt[3],2)==0)
  323.             {
  324.                 stccpy(themsgSIBuff,argv[i]+2,min(54,strlen(argv[i])-1));
  325.                 MakeAlertMsg();
  326.             }
  327.             if(strcmp(argv[i],opt[5])==0 &&_Backstdout)
  328.             {
  329.                 Write(_Backstdout,Usage,sizeof(Usage));
  330.                 Write(_Backstdout,Usage2,sizeof(Usage2));
  331.                 Write(_Backstdout,Usage3,sizeof(Usage3));
  332.                 Write(_Backstdout,Usage4,sizeof(Usage4));
  333.                 Write(_Backstdout,Usage5,sizeof(Usage5));
  334.                 Write(_Backstdout,Usage6,sizeof(Usage6));
  335.                 Write(_Backstdout,Usage7,sizeof(Usage7));
  336.                 Write(_Backstdout,Usage8,sizeof(Usage8));
  337.                 Write(_Backstdout,Usage9,sizeof(Usage9));
  338.                 fin(0);
  339.             }
  340.             if(strnicmp(argv[i],opt[4],2)==0)
  341.             {
  342.                 sscanf(argv[i],"-h%d%*c%d%*c%d",&aa,&bb,&cc);
  343.                 Hr=TRUE;
  344.             }
  345.             if(stricmp(opt[6],argv[i])==0) win2=TRUE;
  346.             if(stricmp(opt[7],argv[i])==0) reb=TRUE;
  347.             if(strnicmp(argv[i],opt[8],2)==0)
  348.             {
  349.                 if(strlen(argv[i])>2) sscanf(argv[i],"-d%d",&duree);
  350.             }
  351.         }
  352.     }
  353.     else
  354.     {
  355.         WBS=(struct WBStartup *)argv;
  356.         IconBase=(struct IconBase *)OpenLibrary(ICONNAME,0);
  357.         if(IconBase!=NULL)
  358.         {    
  359.             DiskObj=GetDiskObject(WBS->sm_ArgList[0].wa_Name);
  360.             if(DiskObj!=0)
  361.             {
  362.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[0]);
  363.                 if(Value!=0) Sec=TRUE;
  364.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[1]);
  365.                 if(Value!=0) Alm=TRUE;
  366.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[2]);
  367.                 if(Value!=0)
  368.                 {
  369.                     stccpy(themsgSIBuff,Value,min(54,strlen(Value)+1));
  370.                     MakeAlertMsg();
  371.                 }
  372.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[3]);
  373.                 if(Value!=0)
  374.                 {
  375.                     sscanf(Value,"%d%*c%d%*c%d",&aa,&bb,&cc);
  376.                     Hr=TRUE;
  377.                 }
  378.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[4]);
  379.                 if(Value!=0) win2=TRUE;
  380.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[5]);
  381.                 if(Value!=0) sscanf(Value,"%d",&duree);
  382.                 Value=FindToolType(DiskObj->do_ToolTypes,opt2[6]);
  383.                 if(Value!=0) reb=TRUE;
  384.                 FreeDiskObject(DiskObj);
  385.             } 
  386.             CloseLibrary(IconBase); 
  387.         }
  388.     }
  389.  
  390.     if(Sec)
  391.     {
  392.         l=1;
  393.         mode.Flags|=SELECTED;
  394.         sec.Flags=GADGHCOMP;
  395.     }
  396.     if(Alm)
  397.     {
  398.         onoff.Flags|=SELECTED;
  399.         min.Flags|=GADGDISABLED;
  400.         heure.Flags|=GADGDISABLED;
  401.         sec.Flags=GADGHCOMP|GADGDISABLED;
  402.     }
  403.     if(Hr)
  404.         if (conf(24,aa) && conf(60,bb) && conf(60,cc))
  405.         {
  406.             heureSInfo.LongInt=aa;
  407.             minSInfo.LongInt=bb;
  408.             secSInfo.LongInt=cc;
  409.             sprintf(heureSInfo.Buffer,"%d",aa);
  410.             sprintf(minSInfo.Buffer,"%d",bb);
  411.             sprintf(secSInfo.Buffer,"%d",cc);
  412.             heureSInfo.NumChars=strlen(heureSInfo.Buffer);
  413.             heureSInfo.BufferPos=heureSInfo.NumChars;
  414.             minSInfo.NumChars=strlen(minSInfo.Buffer);
  415.             minSInfo.BufferPos=minSInfo.NumChars;
  416.             secSInfo.NumChars=strlen(secSInfo.Buffer);
  417.             secSInfo.BufferPos=secSInfo.NumChars;
  418.         }
  419.         else
  420.         {    if(argc!=0) Write(_Backstdout,hello,sizeof(hello));
  421.             AutoRequest(NULL, &my_body_text, NULL, &my_ok_text, NULL, NULL, 200, 50);
  422.             muet=TRUE;
  423.         }
  424.     
  425.     if(_Backstdout && !muet && argc!=0)
  426.         Write(_Backstdout,hello,sizeof(hello));
  427.     if(_Backstdout)    Close(_Backstdout);
  428. }
  429.  
  430.  
  431. void main(int argc, char **argv)
  432. {
  433.     ULONG Class,seconds,micros,sec1=0,mic1=0,sec2=0,mic2=0;
  434.     USHORT Code;
  435.     long Sig;
  436.     int k=0;
  437.     APTR Address;
  438.  
  439.     BOOL closeme=FALSE,late=FALSE;
  440.     struct temps *h;
  441.  
  442.     Args(argc,argv);
  443.  
  444.     init();
  445.     if(reb) rebours();
  446.     fenetre();
  447.     free(Time());
  448.  
  449.     launchtimer(l);
  450.  
  451.     while(!closeme)
  452.     {
  453.         Sig=Wait(1<<Window->UserPort->mp_SigBit|1<<TimerPort->mp_SigBit|1<<Window2->UserPort->mp_SigBit);
  454.         if(Sig & WSMask)
  455.         {
  456.             while(Message=(struct IntuiMessage *)
  457.                 GetMsg(Window->UserPort))
  458.             {
  459.                 Class=Message->Class;
  460.                 Code=Message->Code;
  461.                 seconds = Message->Seconds;
  462.                       micros  = Message->Micros;
  463.                 if(!wb2) Delay(1);
  464.                 ReplyMsg(Message);
  465.                 switch(Class)
  466.                 {
  467.                     case MOUSEBUTTONS:
  468.                         if( Code == MENUDOWN )
  469.                                    {
  470.                             sec2 = sec1;
  471.                              mic2 = mic1;
  472.                             sec1 = seconds;
  473.                             mic1 = micros;
  474.                             if( DoubleClick( sec2, mic2, sec1, mic1 ) && Window2==NULL )
  475.                             {
  476.                                 sec1 = 0;
  477.                                 mic1 = 0;
  478.                                 fenetre2();
  479.                             }
  480.                         }
  481.                         break;
  482.                     case CLOSEWINDOW:
  483.                         closeme=TRUE;
  484.                         break;
  485.                 }
  486.             }
  487.             if(!wb2) stop();
  488.         }
  489.         if((Sig & W2SMask) && Window2!=NULL)
  490.         {
  491.             while(Message=(struct IntuiMessage *)
  492.                 GetMsg(Window2->UserPort))
  493.             {
  494.                 Class=Message->Class;
  495.                 Code=Message->Code;
  496.                 Address=Message->IAddress;
  497.                 ReplyMsg(Message);
  498.                 if(Class==GADGETUP)
  499.                 {
  500.                     k=0;
  501.                     if(Address==(APTR) &heure)
  502.                         if(conforme(24,&heureSInfo))
  503.                             ActivateGadget(&min,Window2,NULL);
  504.                         else
  505.                             oops(&heure,&heureSInfo);
  506.                     if(Address==(APTR) &min)
  507.                         if(conforme(60,&minSInfo))
  508.                         {
  509.                             if(mode.Flags & SELECTED)  ActivateGadget(&sec,Window2,NULL);
  510.                         }
  511.                         else
  512.                             oops(&min,&minSInfo);
  513.                             
  514.                     if(Address==(APTR) &sec)
  515.                         if(!conforme(60,&secSInfo))
  516.                             oops(&sec,&secSInfo);
  517.                     if(Address==(APTR) &onoff)
  518.                         if(onoff.Flags & SELECTED)
  519.                         {
  520.                             OffGadget(&heure,Window2,NULL);
  521.                             OffGadget(&min,Window2,NULL);
  522.                             OffGadget(&sec,Window2,NULL);
  523.                         }
  524.                         else
  525.                         {
  526.                             OnGadget(&heure,Window2,NULL);
  527.                             OnGadget(&min,Window2,NULL);
  528.                             if(mode.Flags & SELECTED)
  529.                                 OnGadget(&sec,Window2,NULL);
  530.                         }
  531.  
  532.                     if(Address==(APTR) &mode)
  533.                     {
  534.                         l=abs(l-1);
  535.                         if(mode.Flags & SELECTED)
  536.                         { 
  537.                             if(!(onoff.Flags & SELECTED))
  538.                                 OnGadget(&sec,Window2,NULL);
  539.                             if(l) stop();
  540.                         }
  541.                         else
  542.                         {
  543.                             stop();
  544.                             MyIT.IText="        ";
  545.                             if(!wb2) PrintIText(RPort,&MyIT,30,1);
  546.                             OffGadget(&sec,Window2,NULL);
  547.                         }
  548.                     }
  549.                     if(Address==(APTR) &cntdwn)
  550.                         k=3;
  551.                     if(Address==(APTR) &chscr)
  552.                         k=2;
  553.                     if(Address==(APTR) &OK)
  554.                         k=1;
  555.                 }
  556.             }
  557.         }
  558.         if(k!=0) 
  559.         {
  560.             late=fonction(k,l);
  561.             k=0;
  562.         }
  563.  
  564.         if(TimeSigMask & Sig)
  565.         {
  566.             h=Time();
  567.             if(((h->hr*3600+h->min*60+h->sec==(secSInfo.LongInt+minSInfo.LongInt*60+heureSInfo.LongInt*3600)) && (onoff.Flags & SELECTED)) || late)
  568.             {            
  569.                 late=FALSE;
  570.                 MakeAlertMsg();
  571.                 DisplayBeep(screen);
  572.                 DisplayAlert(DEADEND_ALERT,alert,75);
  573.                 if(Window2!=NULL)
  574.                 {
  575.                     ffen2();
  576.                     fantom();
  577.                     fenetre2();
  578.                 }
  579.                 else
  580.                     fantom();
  581.             }
  582.             launchtimer(l);
  583.             free(h);
  584.         }
  585.     }
  586.     stop();
  587.     ffen();
  588.     fin(4);
  589. }
  590.